package exec;

import java.io.File;

public class ConvertToFlv {
	private static String mencoderPath = "D:\\newsite\\DeepRed_admin\\flvTools\\mencoder.exe";

	private static String ffmpegPath = "D:\\newsite\\DeepRed_admin\\flvTools\\ffmpeg.exe";

	public static void genSnapshot(String inPath, String outPath, int iWidth, int iheight, StringBuffer logBuf) {
		StringBuffer stBuf = new StringBuffer();
		stBuf.append(ffmpegPath);
		stBuf.append(" -i " + inPath);
		stBuf.append(" -y -f image2 -sameq -t 0.001 -ss 2 ");
		if(iWidth > 0)stBuf.append("-s " + iWidth + "x" + iheight + " ");
		stBuf.append(outPath);
		RuntimeExec.exec(stBuf.toString(), null, null, logBuf);
	}

	public static void convert(String inPath, String outPath) {
		String inPath1 = inPath.toLowerCase();
		if (inPath1.endsWith(".flv")){
			File tFile = new File(inPath);
			tFile.renameTo(new File(outPath));
		}
		else if (inPath1.endsWith(".rm") || inPath1.endsWith(".rmvb")
				|| inPath1.endsWith(".wmv") || inPath1.endsWith(".asf"))
			convert1(inPath, outPath);
		else
			convert2(inPath, outPath);
	}

	public static void convert1(String inPath, String outPath) {
		StringBuffer stBuf = new StringBuffer();
		stBuf.append(mencoderPath);
		stBuf.append(" " + inPath);
		stBuf.append(" -o " + outPath);
		stBuf.append(" -of lavf -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=200:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=4:cmp=6:vb_strategy=1 -ofps 12 -srate 22050");  //-vf scale=320:240 
		RuntimeExec.exec(stBuf.toString(), null, null, null);
	}

	public static void convert2(String inPath, String outPath) {
		StringBuffer stBuf = new StringBuffer();
		stBuf.append(ffmpegPath);
		stBuf.append(" -i " + inPath);
		stBuf.append(" -y -ab 56 -ar 11025 -ac 2 -b 1500 -qscale 9 -r 15 -s 320*240 ");
		stBuf.append(outPath);
		RuntimeExec.exec(stBuf.toString(), null, null, null);
	}

	public static void main(String[] args) {
		String inPath = "D:\\newsite\\DeepRed\\WebRoot\\video\\upload\\4.rmvb";
		StringBuffer logBuf = new StringBuffer();
		RuntimeExec.exec("cmd /c dir", null, null, logBuf);
		convert(inPath, "D:\\temp\\test.flv");
		//RuntimeExec.exec("ping www.chinamec.cn", null, null, null);
		//genSnapshot(inPath, "E:\\temp\\test-120x72.jpg", 120, 72, logBuf);
		//genSnapshot("E:\\temp\\test.flv", "E:\\temp\\test.jpg", 0, 0, null);
		//System.out.println(logBuf.toString());
	}

}